//
//  TransmissionViewController.m
//  GenericProject
//
//  Created by 大鹏 on 2022/7/29.
//  Copyright © 2022 漠然丶情到深处. All rights reserved.
//

#import "TransmissionViewController.h"
#import "GCDWebUploader.h"
#import "GetMIPHelper.h"

@interface TransmissionViewController ()<GCDWebUploaderDelegate>

@property(nonatomic,strong)GCDWebUploader *webServer;
/* 显示ip地址 */
@property(nonatomic,strong)UILabel *showIpLabel;
/* fileArray */
@property(nonatomic,strong)NSMutableArray *fileArray;

@end

@implementation TransmissionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigation];
    [self analysis];
    [self createSubViews];
}

- (void)setNavigation {
    self.navView.leftButtonTitle = @"";
    self.navView.titleLabelText = @"WIFI导入";
    self.navView.isShowRightButton = NO;
    self.navView.isShowNavViewLine = NO;
}

- (void)analysis {
    self.fileArray = [NSMutableArray array];
}

- (void)createSubViews {
    // 文件存储位置
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    // 创建webServer，设置根目录
    self.webServer = [[GCDWebUploader alloc] initWithUploadDirectory:documentsPath];
    // 设置代理
    self.webServer.delegate = self;
    self.webServer.allowHiddenItems = YES;
    // 限制文件上传类型
    self.webServer.allowedFileExtensions = @[@"txt"];
    // 设置网页标题
    self.webServer.title = NSStringFormat(@"%@的阅读器",iPhoneName);
    // 设置展示在网页上的文字(开场白)
    self.webServer.prologue = NSStringFormat(@"欢迎使用 %@ 的WIFI管理平台",iPhoneName);
    // 设置展示在网页上的文字(收场白)
    self.webServer.epilogue = NSStringFormat(@"%@制作",iPhoneName);
    
    self.showIpLabel = [[UILabel alloc] init];
    self.showIpLabel.font = LSFont(@"Regular", LSSYRealValue(14));
    self.showIpLabel.textAlignment = NSTextAlignmentCenter;
    self.showIpLabel.textColor = [UIColor lsLigthColor:LSBlackColor darkColor:LSWhiteColor];
    [self.view addSubview:self.showIpLabel];
    [self.showIpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(LSNAVIGATION_BAR_HEIGHT);
        make.left.equalTo(self.view).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(LSScreenWidth, LSSYRealValue(50)));
    }];
    
    if ([self.webServer start]) {
        self.showIpLabel.text = NSStringFormat(@"请在网页输入这个地址  http://%@:%ld", [GetMIPHelper deviceIPAdress], self.webServer.port);
    } else {
        self.showIpLabel.text = NSLocalizedString(@"GCDWebServer not running!", nil);
    }

    self.mainTableView.frame = CGRectMake(0, LSNAVIGATION_BAR_HEIGHT + LSSYRealValue(50) , LSScreenWidth, LSScreenHeight - LSSYRealValue(50) - LSNAVIGATION_BAR_HEIGHT - LSHOME_INDICATOR_HEIGHT);
    [self.view addSubview:self.mainTableView];
}

#pragma mark - <GCDWebUploaderDelegate>
- (void)webUploader:(GCDWebUploader *)uploader didUploadFileAtPath:(NSString *)path {
    LSNSLog(@"[UPLOAD] %@", path);

    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.fileArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
    
    [self.mainTableView reloadData];
    
    LSSendNotification(@"upDateList");
}

- (void)webUploader:(GCDWebUploader *)uploader didMoveItemFromPath:(NSString *)fromPath toPath:(NSString *)toPath {
    LSNSLog(@"[MOVE] %@ -> %@", fromPath, toPath);
}

- (void)webUploader:(GCDWebUploader *)uploader didDeleteItemAtPath:(NSString *)path {
    LSNSLog(@"[DELETE] %@", path);
    
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.fileArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
    
    [self.mainTableView reloadData];
    
    LSSendNotification(@"upDateList");
}

- (void)webUploader:(GCDWebUploader *)uploader didCreateDirectoryAtPath:(NSString *)path {
    LSNSLog(@"[CREATE] %@", path);
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.fileArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = self.fileArray[indexPath.row];

    return cell;
}

- (void)dealloc {
    [self.webServer stop];
    self.webServer = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
